package neu.edu.vn.appmusic.ui;

import java.util.ArrayList;

import neu.edu.vn.appmusic.R;
import neu.edu.vn.appmusic.adapter.SongAlbumAdapter;
import neu.edu.vn.appmusic.common.Constant;
import neu.edu.vn.appmusic.object.Song;
import neu.edu.vn.appmusic.utils.ImageUtils;
import neu.edu.vn.appmusic.utils.MyApplication;
import neu.edu.vn.appmusic.utils.music.MyMusic;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;



public class ActivityDetailAlbum extends Activity {
	
	private ImageView ivAlbumDetails;
	private TextView tvAlbumNameDetail;
	private TextView tvNumberView;
	private TextView tvNumberLike;
	private TextView tvAlbumSinger;
	
	private ListView lvAlbumDetails;
	private ArrayList<Song> listSong;
	private SongAlbumAdapter adapter;
	MyMusic myDetailMusic = MyApplication.getInstance().getMyMusic();
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_album_details);
		Intent intent = getIntent();
		int albumId = intent.getIntExtra("albumId", 0);
		initGUI();
		loadDataFormServer(albumId);
		
	}
	
	private void initGUI(){
		ivAlbumDetails = (ImageView) findViewById(R.id.ivAlbumDetails);
		tvAlbumNameDetail = (TextView) findViewById(R.id.tvAlbumNameDetail);
		tvAlbumSinger = (TextView) findViewById(R.id.tvAlbumSinger);
		lvAlbumDetails = (ListView) findViewById(R.id.lvAlbumDetails);
		tvNumberLike = (TextView) findViewById(R.id.tvNumberLike);
		tvNumberView = (TextView) findViewById(R.id.tvNumberView);
		listSong = new ArrayList<Song>();
		adapter = new SongAlbumAdapter(ActivityDetailAlbum.this, R.layout.item_list_song_album, listSong);
		lvAlbumDetails.setAdapter(adapter);
		lvAlbumDetails.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				myDetailMusic.initMusic(ActivityDetailAlbum.this,R.id.lvAlbumDetails,listSong);
				songPicked(position);
			}
		});
	}
	
	private void loadDataFormServer(int albumId){
		RequestQueue queue = Volley.newRequestQueue(ActivityDetailAlbum.this);
		String url = Constant.API_GET_ALBUM_DETAIL+albumId;
		Log.i("INFOO", url);
		StringRequest request = new StringRequest(url, new Listener<String>() {

			@Override
			public void onResponse(String result) {
				try {
					JSONObject jsonObject= new JSONObject(result);
					Log.i("INFOO", "REsult: " + result);
					JSONObject jsonResult = jsonObject.getJSONObject("result");
					
					JSONObject jsonAlbum = jsonResult.getJSONObject("data");
					tvAlbumNameDetail.setText(jsonAlbum.getString("albumName"));
					tvAlbumSinger.setText(" "+jsonAlbum.getString("albumSinger"));
					tvNumberLike.setText(" "+jsonAlbum.getString("like"));
					tvNumberView.setText(" "+jsonAlbum.getString("view"));
					ImageUtils.getInstance(ActivityDetailAlbum.this).loadImageToImageview(jsonAlbum.getString("albumSrc"), ivAlbumDetails);
					
					JSONArray data = jsonResult.getJSONArray("album_songs");
					Song song;
					for (int i = 0; i < data.length(); i++) {
						song = new Song();
						JSONObject jsonSong = data.getJSONObject(i);
						song.setId(jsonSong.getInt("id"));
						song.setSongName(jsonSong.getString("name"));
						song.setAuthor(jsonSong.getString("singer"));
						song.setSrc(jsonSong.getString("src"));
						listSong.add(song);
					}
					if(adapter != null){
						adapter.notifyDataSetChanged();
					}
				} catch (Exception e) {
					Log.e("Exception", e.getMessage(), e);
				}
			}
		}, new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError arg0) {
				// TODO Auto-generated method stub
				Log.e("Exception", arg0.getMessage(), arg0);
			}
		});
		queue.add(request);
	}
	
	//user song select
	public void songPicked(int position){
		myDetailMusic.songPicked(position);
	}
	
//	@Override
//	public void onPause(){
//		super.onPause();
//		myDetailMusic.onPause();
//	}
//
//	@Override
//	public void onResume(){
//		super.onResume();
//		myDetailMusic.onResume();
//	}
//
//	@Override
//	public void onStop() {
//		myDetailMusic.onStop();
//		super.onStop();
//	}
//
//	@Override
//	public void onDestroy() {
//		myDetailMusic.onDestroy();
//		super.onDestroy();
//	}
}
